package com.eshop.web.controllers.api;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.eshop.frameworks.core.controller.BaseController;
import com.eshop.frameworks.core.entity.PageEntity;
import com.eshop.model.mongodb.ShopAndGoods;
import com.eshop.service.mongodb.EShopService;
import com.eshop.service.mongodb.ShopAndGoodsService;

@Controller
@RequestMapping("/api")
public class GoodsApiController extends BaseController {
	private static final Logger logger = Logger.getLogger(GoodsApiController.class);
	@Autowired
	private ShopAndGoodsService shopAndGoodsService;
	@Autowired
	private EShopService eshopService;
	
	@RequestMapping(value="/enterShop",method = RequestMethod.GET)
	@ResponseBody
	public String enterShop(@RequestParam(value="shopId")String shopId,@RequestParam(value="typeCode",required=false)String typeCode,PageEntity page, HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();
		try {
			this.setPage(page);
			this.getPage().setPageSize(20);
			ShopAndGoods query = new ShopAndGoods();
			query.setStatus(1);
			query.setTypeCode(typeCode);
			List<ShopAndGoods> list = shopAndGoodsService.getShopperGoods(shopId, query, page);
			jsonObject.put("currentPage", page.getCurrentPage());
			jsonObject.put("resluts", list);
			jsonObject.put("success", true);
			jsonObject.put("totalResult", page.getTotalResultSize());
			jsonObject.put("totalPage", page.getTotalPageSize());
			jsonObject.put("currentPage", page.getCurrentPage());
			jsonObject.put("timeStamp", System.currentTimeMillis());
		} catch (Exception e) {
			logger.error("GoodsApiController.enterShop", e);
		}
		return jsonObject.toJSONString();
	}
	
}
